import { is_empty, filter_row, obj_str, loc, add_ck_col, reload_col, ck_sn, ck_empty, cline, uDb } from 'sui';

const load_type = (setType, mod) => uDb(null, `file/list/${mod}`, {line: cline()}, r => setType(r), e => setType(null));

const load_wtype = (setType) => load_type(setType, "wtype");

const get_fix = () => {
    return [
        {sn: "sn", name: loc("l_405"), tp: "string"},
        {sn: "name", name: loc("l_250"), tp: "string"},
        {sn: "tp", name: loc("l_251"), tp: "string", ctp: "list", ctl: null},
    ];
};

const get_ck = (cur, main, col) => {
    const ret = {
        sn : v => ck_sn(v, cur, main),
        name : v => ck_empty(v),
        tp : v => ck_empty(v),
    };

    add_ck_col(ret, col);

    return ret;
};

const load_col = (setCol) => {
    const fix = get_fix();
    reload_col(null, v => setCol(v), "station", fix);
};

const load_item = (hDone, cur, setUser, cmd="getuser", mod="station") => {
    uDb(hDone, `${mod}/${cmd}`,
        {
            line: cline(),
            k: obj_str(cur, "sn"),
        }, r => setUser(r), e => setUser(null));
};

const filter_item = (main, data, filter) => {
    if (is_empty(main)) {
        return null;
    }

    let user = [...main];

    if (!is_empty(data)) {
        user = main.filter(d => {
            if (data.find(it => it === d.sn)) {
                return false;
            }
            return true;
        });
    }

    const flst = ["name", "sn"];
    user = filter_row(user, flst, filter);

    if (is_empty(user)) {
        return null;
    }

    return user;
};

export {filter_item, load_type, load_item, load_col, get_ck, get_fix, load_wtype};
